package com.car.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.order.entity.GoodOrderItem;
import com.car.order.entity.form.goodOrderItem.GoodOrderItemFindListForm;
import com.car.order.entity.form.goodOrderItem.GoodOrderItemFindPageForm;
import com.car.order.entity.form.goodOrderItem.GoodOrderItemInsertForm;
import com.car.order.entity.form.goodOrderItem.GoodOrderItemUpdateForm;
import com.car.order.mapper.GoodOrderItemMapper;
import com.car.order.service.GoodOrderItemService;
import com.car.commons.core.model.WoniuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * @author 杜岷芫
 * @since 2024-08-28 19:11:04
 */
@Service
@Slf4j
public class GoodOrderItemServiceImpl extends ServiceImpl<GoodOrderItemMapper, GoodOrderItem> implements GoodOrderItemService {

    @Autowired
    private GoodOrderItemMapper goodOrderItemMapper;

    @Override
    public Page<GoodOrderItem> findPage(GoodOrderItemFindPageForm form) {
        Page<GoodOrderItem> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<GoodOrderItem> query = new QueryWrapper<>();
        return goodOrderItemMapper.selectPage(page, query);
    }

    @Override
    public List<GoodOrderItem> findList(GoodOrderItemFindListForm form){
        QueryWrapper<GoodOrderItem> query = new QueryWrapper<>();
        return goodOrderItemMapper.selectList(query);
    }

    @Override
    public GoodOrderItem findById(Integer id) {
        return goodOrderItemMapper.selectById(id);
    }

    @Override
    public GoodOrderItem insert(GoodOrderItemInsertForm form) {
        GoodOrderItem goodOrderItem = BeanUtil.toBean(form, GoodOrderItem.class);
        boolean save = save(goodOrderItem);
        if (!save) {
            throw new WoniuException(500, "新增失败");
        }
        return goodOrderItem;
    }

    @Override
    public GoodOrderItem update(GoodOrderItemUpdateForm form) {
        GoodOrderItem goodOrderItem = BeanUtil.toBean(form, GoodOrderItem.class);
        boolean b = updateById(goodOrderItem);
        if (!b) {
            throw new WoniuException(500, "修改失败");
        }
        return goodOrderItem;
    }

    @Override
    public void delete(Integer id) {
        Integer i=goodOrderItemMapper.deleteById(id);
        if (i == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }

}